Clean up some of the more mechanical uses of time_t and implicit conversions.
authorrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Mon, 29 Jul 2013 00:23:34 +0000 (00:23 +0000)
committerrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Mon, 29 Jul 2013 00:23:34 +0000 (00:23 +0000)
git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@4478 f51c46e8-681c-474f-0cfe-069cfd0219fb

gpsbabel/arcdist.cc
gpsbabel/csv_util.cc
gpsbabel/garmin.cc
gpsbabel/garmin_fit.cc
gpsbabel/lowranceusr4.cc
gpsbabel/maggeo.cc
gpsbabel/mapasia.cc
gpsbabel/mmo.cc
gpsbabel/pocketfms_bc.cc
gpsbabel/smplrout.cc
gpsbabel/waypt.cc

index 945f29f5b6e0c5b5f170afe166a5e242c50818a1..bfb4cea001faa1e47599d767afaafaec60e1f1fd 100644 (file)
@@ -234,14 +234,14 @@ arcdist_process(void)
              (ptsopt || (ed->arcpt1->GetCreationTime().isValid()))) {
           /* Interpolate time */
           if (ptsopt) {
-            wp->creation_time = ed->arcpt2->creation_time;
+            wp->SetCreationTime(ed->arcpt2->GetCreationTime());;
           } else {
             // Apply the multiplier to the difference between the times 
             // of the two points.   Add that to the first for the 
             // interpolated time.
             int scaled_time = ed->frac * 
-                  ed->arcpt1->creation_time.msecsTo(ed->arcpt2->creation_time);
-            QDateTime new_time(ed->arcpt1->creation_time.addMSecs(scaled_time));
+                  ed->arcpt1->GetCreationTime().msecsTo(ed->arcpt2->GetCreationTime());
+            QDateTime new_time(ed->arcpt1->GetCreationTime().addMSecs(scaled_time));
             wp->SetCreationTime(new_time);
           }
         }
index 0adcba66cb373fe0d4f83e06dade0a4b9a05534f..051d4619c67b419601b7da83266d46d9b184677b 100644 (file)
@@ -831,29 +831,14 @@ xcsv_epilogue_add(char* epilogue)
 }
 
 static
-time_t
+QDateTime
 yyyymmdd_to_time(const char* s)
 {
-  int t = atol(s);
-  struct tm tm;
-
-  memset(&tm, 0, sizeof(tm));
-
-  tm.tm_mday = t % 100;
-  t = t / 100;
-  tm.tm_mon = t % 100 - 1;
-  t = t / 100;
-  tm.tm_year = t - 1900;
-
-  if (mkgmtime(&tm) > 0) {
-    return mktime(&tm);
-  } else {
-    return 0;
-  }
+  QDate d = QDate::fromString(s, "yyyyMMdd");
+  return QDateTime(d);
 }
 
 
-
 /*
  * sscanftime - Parse a date buffer using strftime format
  */
@@ -924,26 +909,6 @@ writetime(char* buff, size_t bufsize, const char* format, time_t t, bool gmt)
   return strftime(buff, bufsize, format, stmp);
 }
 
-#if 0
-/* not used */
-static
-int
-writeisotime(char* buff, size_t bufsize, const char* format, time_t t)
-{
-  static struct tm* stmp;
-  char* ibuff = NULL;
-  int i;
-
-  ibuff = xmalloc(bufsize);
-  stmp = gmtime(&t);
-  strftime(ibuff, bufsize, format, stmp);
-  i = snprintf(buff, bufsize, format, ibuff);
-  xfree(ibuff);
-  return i;
-}
-#endif
-
-
 static
 int
 writehms(char* buff, size_t bufsize, const char* format, time_t t, int gmt)
@@ -968,16 +933,10 @@ writehms(char* buff, size_t bufsize, const char* format, time_t t, int gmt)
 
 static
 long
-time_to_yyyymmdd(time_t t)
+time_to_yyyymmdd(QDateTime t)
 {
-  long b;
-  struct tm* tm = gmtime(&t);
-
-  b = (1900 + tm->tm_year) * 10000 +
-      (1 + tm->tm_mon) * 100 +
-      tm->tm_mday;
-
-  return b;
+ QDate d = t.date();
+ return d.year() * 10000 + d.month() * 100 + d.day();
 }
 
 static garmin_fs_t*
@@ -1218,7 +1177,7 @@ xcsv_parse_val(const char* s, waypoint* wpt, const field_map_t* fmp,
     break;
   case XT_ISO_TIME:
   case XT_ISO_TIME_MS:
-    wpt->creation_time = xml_parse_time(s);
+    wpt->SetCreationTime(xml_parse_time(s));
     break;
   case XT_NET_TIME: {
     time_t tt = wpt->GetCreationTime();
@@ -1939,13 +1898,13 @@ xcsv_waypt_pr(const waypoint* wpt)
       break;
     case XT_TIMET_TIME:
       /* time as a time_t variable */ {
-      time_t tt = wpt->GetCreationTime();
+      time_t tt = wpt->GetCreationTime().toTime_t();
       writebuff(buff, fmp->printfc, tt); }
       break;
     case XT_TIMET_TIME_MS: {
       /* time as a time_t variable in milliseconds */
       char tbuf[24];
-      writetime(tbuf, sizeof(tbuf), "%s", wpt->GetCreationTime(), false);
+      writetime(tbuf, sizeof(tbuf), "%s", wpt->GetCreationTime().toTime_t(), false);
       char mbuf[32];
       snprintf(mbuf, sizeof(mbuf), "%s%03d", tbuf, wpt->GetCreationTime().time().msec());
       writebuff(buff, "%s", mbuf);
index 30724c66b8e6bce3e0a935888334b1a867672d86..eab301650ce2c4d3508843f25747a6a0fb5e3e6f 100644 (file)
@@ -983,8 +983,9 @@ waypoint_prepare(void)
     } else {
       tx_waylist[i]->alt = wpt->altitude;
     }
-    if (wpt->creation_time) {
-      tx_waylist[i]->time = wpt->GetCreationTime();
+    gpsbabel::DateTime t = wpt->GetCreationTime();
+    if (t.isValid()) {
+      tx_waylist[i]->time = t.toTime_t();
       tx_waylist[i]->time_populated = 1;
     }
     if (category) {
@@ -1135,7 +1136,7 @@ track_waypt_pr(const waypoint* wpt)
   (*cur_tx_tracklist_entry)->lat = wpt->latitude;
   (*cur_tx_tracklist_entry)->lon = wpt->longitude;
   (*cur_tx_tracklist_entry)->alt = (wpt->altitude != unknown_alt) ? wpt->altitude : 1e25;
-  (*cur_tx_tracklist_entry)->Time = wpt->creation_time;
+  (*cur_tx_tracklist_entry)->Time = wpt->GetCreationTime().toTime_t();;
   if (wpt->shortname) {
     strncpy((*cur_tx_tracklist_entry)->trk_ident, wpt->shortname, sizeof((*cur_tx_tracklist_entry)->trk_ident));
     (*cur_tx_tracklist_entry)->trk_ident[sizeof((*cur_tx_tracklist_entry)->trk_ident)-1] = 0;
index 14bc02baa5396410330924cb074d3bcb5a0bbec4..4e72886f89dd8f842e370210c75b1ba08d216c2b 100644 (file)
@@ -376,7 +376,7 @@ fit_parse_data(fit_message_def* def, int time_offset)
     if (alt != 0xffff) {
       waypt->altitude = (alt / 5.0) - 500;
     }
-    waypt->creation_time = timestamp + 631065600;
+    waypt->SetCreationTime(QDateTime::fromTime_t(timestamp + 631065600));
     if (speed != 0xffff) {
       WAYPT_SET(waypt, speed, speed / 1000.0f);
     }
index db8ccfaa37e27f0b39f283c7a0530e609bb5a7a4..8fd978177a1a82c672e66a3489ff125d0c8769df 100644 (file)
@@ -689,7 +689,7 @@ lowranceusr4_parse_trails(void)
       gbfgetc(file_in);
 
       /* POSIX timestamp */
-      wpt_tmp->creation_time = gbfgetint32(file_in);
+      wpt_tmp->SetCreationTime(QDateTime::fromTime_t(gbfgetint32(file_in)));
 
       /* Long/Lat */
       wpt_tmp->longitude = gbfgetdbl(file_in) / DEGREESTORADIANS; /* rad to deg */
index a3b7f43daf45930ea27b6107f3bcc3bde0fbf8b4..d4623acd3d428a00426ce7da176edad74d33bff9 100644 (file)
@@ -36,7 +36,7 @@ static gbfile* maggeofile_in;
 static gbfile* maggeofile_out;
 static short_handle desc_handle = NULL;
 
-static time_t maggeo_parsedate(char* dmy);
+static QDateTime maggeo_parsedate(char* dmy);
 
 static void
 maggeo_writemsg(const char* const buf)
@@ -173,29 +173,16 @@ maggeo_read(void)
 
 }
 
-/*
- * Note: returns allocated buffer that must be freed by caller.
- */
 static
-char*
-maggeo_fmtdate(time_t t)
+QString
+maggeo_fmtdate(QDateTime dt)
 {
-#define SZ 16
-
-  struct tm* tm = NULL;
-  int date;
-  char* cbuf = (char*) xmalloc(SZ);
-
-  cbuf[0] = '\0';
-  if (t > 0) {
-    tm = localtime(&t);
-    if (tm) {
-      date = tm->tm_mday * 100000 + (1+tm->tm_mon) * 1000 +
-             tm->tm_year;
-      snprintf(cbuf, SZ, "%07d", date);
-    }
-  }
-  return cbuf;
+  QDate date = dt.date();
+  int y = date.year() - 1900;
+  int m = date.month();
+  int d = date.day();
+  int r = d * 100000 + m * 1000 + y;
+  return QString("%1").arg(r, 7, 10, QChar('0'));
 }
 
 /*
@@ -204,31 +191,14 @@ maggeo_fmtdate(time_t t)
  * century is three digits but anything from before 2000, we'd have
  * two digit years.  This makes this easier to parse as strings.
  */
-static time_t maggeo_parsedate(char* dmy)
+static QDateTime maggeo_parsedate(char* dmy)
 {
-  struct tm tm;
-  char dd[3];
-  char mm[3];
-
-  if (strlen(dmy) < 5) {
-    return 0;
-  }
-
-  memset(&tm, 0, sizeof(tm));
-
-  dd[0] = dmy[0];
-  dd[1] = dmy[1];
-  dd[2] = 0;
-
-  mm[0] = dmy[2];
-  mm[1] = dmy[3];
-  mm[2] = 0;
-
-  tm.tm_mday = atoi(dd);
-  tm.tm_mon = atoi(mm) - 1;
-  tm.tm_year = atoi(dmy + 4);
-
-  return mktime(&tm);
+  QString date(dmy);
+  int d = date.mid(0,2).toInt();
+  int m = date.mid(2,2).toInt();
+  int y = date.mid(4,3).toInt();
+  QDateTime r(QDate (y + 1900, m, d));
+  return r;
 }
 
 /*
@@ -273,8 +243,6 @@ maggeo_waypt_pr(const waypoint* waypointp)
   char* cname = NULL;
   const char* ctype = NULL;
   QString placer;
-  char* lfounddate = NULL;
-  char* placeddate = NULL;
 
   ilat = waypointp->latitude;
   ilon = waypointp->longitude;
@@ -301,9 +269,10 @@ maggeo_waypt_pr(const waypoint* waypointp)
   } else {
     ctype = gs_get_cachetype(waypointp->gc_data->type);
   }
-  placeddate = maggeo_fmtdate(waypointp->creation_time);
-  lfounddate = maggeo_fmtdate(waypointp->gc_data->last_found);
-  cname = mkshort(desc_handle, waypointp->notes ? waypointp->notes : waypointp->description);
+  QString placeddate = maggeo_fmtdate(waypointp->creation_time);
+  QString lfounddate = maggeo_fmtdate(waypointp->gc_data->last_found);
+  cname = mkshort(desc_handle, 
+                  waypointp->notes ? waypointp->notes : waypointp->description);
   placer = waypointp->gc_data->placer;
 
   /*
@@ -329,8 +298,8 @@ maggeo_waypt_pr(const waypoint* waypointp)
   append(obuf, placer.toUtf8().data());
   append(obuf, waypointp->gc_data->hint.toUtf8().data());
   append(obuf, ctype);
-  append(obuf, placeddate);
-  append(obuf, lfounddate);
+  append(obuf, placeddate.toUtf8());
+  append(obuf, lfounddate.toUtf8());
 
   if (waypointp->gc_data->diff/10.0)
     sprintf(obuf + strlen(obuf), ",%3.1f",
@@ -346,12 +315,6 @@ maggeo_waypt_pr(const waypoint* waypointp)
     strcat(obuf, ",");
   }
 
-  if (lfounddate) {
-    xfree(lfounddate);
-  }
-  if (placeddate) {
-    xfree(placeddate);
-  }
   if (cname) {
     xfree(cname);
   }
index 7c306a9fd81857a9fc5323742712ea667e9aedef..13caa3ade93788e6c3d59a13ef8f01172ad164ae 100644 (file)
@@ -85,14 +85,11 @@ tr7_read(void)
   while (! gbfeof(fin)) {
     unsigned char buff[TR7_S_SIZE];
     double lat, lon;
-    struct tm tm;
     waypoint* wpt;
     float speed, course;
 
     gbfread(buff, 1, sizeof(buff), fin);
 
-    memset(&tm, 0, sizeof(tm));
-
     lat = (double)le_read32(&buff[TR7_S_LAT]) / 1000000.0;
     lon = (double)le_read32(&buff[TR7_S_LON]) / 1000000.0;
 
@@ -101,17 +98,13 @@ tr7_read(void)
       continue;
     }
 
-    tm.tm_year = le_read16(&buff[TR7_S_YEAR]);
-    tm.tm_mon = buff[TR7_S_MONTH];
-    tm.tm_mday = buff[TR7_S_DAY];
-    tm.tm_hour = buff[TR7_S_HOUR];
-    tm.tm_min = buff[TR7_S_MIN];
-    tm.tm_sec = buff[TR7_S_SEC];
-
-    if ((tm.tm_mday < 1) || (tm.tm_mday > 31) ||
-        (tm.tm_mon < 1) || (tm.tm_mon > 12) ||
-        (tm.tm_year <= 1970) ||
-        (tm.tm_year > tmref.tm_year+1)) {
+    QDate date(le_read16(&buff[TR7_S_YEAR]),
+               buff[TR7_S_MONTH],
+               buff[TR7_S_DAY]);
+    QTime time(buff[TR7_S_HOUR],
+               buff[TR7_S_MIN],
+               buff[TR7_S_SEC]);
+    if (!date.isValid() || !time.isValid()) {
       continue;
     }
 
@@ -126,9 +119,7 @@ tr7_read(void)
     wpt->latitude = lat;
     wpt->longitude = lon;
 
-    tm.tm_year -= 1900;
-    tm.tm_mon -= 1;
-    wpt->creation_time = mkgmtime(&tm);
+    wpt->SetCreationTime(QDateTime(date, time, Qt::UTC));
 
     WAYPT_SET(wpt, course, course);
     WAYPT_SET(wpt, speed, speed);
@@ -240,16 +231,18 @@ tr7_disp_waypt_cb(const waypoint* wpt)
     le_write16(&buff[TR7_S_COURSE], (int)(360 - course));
   }
 
-  if (wpt->creation_time) {
-    const time_t tt = wpt->creation_time;
-    tm = *gmtime(&tt);
+  QDateTime dt = wpt->GetCreationTime().toUTC();
+  if (dt.isValid()) {
+    QDate d = dt.date();
+
+    le_write16(&buff[TR7_S_YEAR], d.year());
+    buff[TR7_S_MONTH] = d.month();
+    buff[TR7_S_DAY] = d.day();
 
-    le_write16(&buff[TR7_S_YEAR], tm.tm_year + 1900);
-    buff[TR7_S_MONTH] = tm.tm_mon + 1;
-    buff[TR7_S_DAY] = tm.tm_mday;
-    buff[TR7_S_HOUR] = tm.tm_hour;
-    buff[TR7_S_MIN] = tm.tm_min;
-    buff[TR7_S_SEC] = tm.tm_sec;
+    QTime t = dt.time();
+    buff[TR7_S_HOUR] = t.hour();
+    buff[TR7_S_MIN] = t.minute();
+    buff[TR7_S_SEC] = t.second();
 
     if WAYPT_HAS(wpt, speed) {
       speed = wpt->speed;
index cf91d906ed4dd63409190b44703aa5b1e55527bb..7ebb89d8b9b40d626ae7fba07d7de95e57c1a085 100644 (file)
@@ -173,7 +173,7 @@ static char*
 mmo_readstr(void)
 {
   char* res;
-  unsigned int len;
+  signed int len;
 
   len = (unsigned)gbfgetc(fin);
   if (len == 0xFF) {
@@ -1270,7 +1270,7 @@ mmo_write_wpt_cb(const waypoint* wpt)
   int icon = 0;
   mmo_data_t* data;
 
-  time = wpt->creation_time;
+  time = wpt->GetCreationTime().toTime_t();
   if (time < 0) {
     time = 0;
   }
@@ -1378,9 +1378,9 @@ mmo_write_rte_head_cb(const route_head* rte)
 
   QUEUE_FOR_EACH(&rte->waypoint_list, elem, tmp) {
     waypoint* wpt = (waypoint*)elem;
-
-    if ((wpt->creation_time > 0) && (wpt->creation_time < time)) {
-      time = wpt->creation_time;
+    QDateTime t = wpt->GetCreationTime();
+    if ((t.isValid()) && (t.toTime_t() < time)) {
+      time = t.toTime_t();
     }
   }
   if (time == 0x7FFFFFFF) {
index 652193b9cffeb58de119de682fd465ad1fc3dfe0..306d473b5bc6e22ac69ddd45b64c585e38a0b2b6 100644 (file)
@@ -132,7 +132,7 @@ pocketfms_waypt_disp(const waypoint *wpt)
   struct tm *tm;
 
   memset(&bc, 0, sizeof(bc));
-  const time_t tt = wpt->GetCreationTime();
+  const time_t tt = wpt->GetCreationTime().toTime_t();
   tm = localtime(&tt);
   if (wpt->creation_time) {
     const time_t tt = wpt->GetCreationTime();
index e0d49cd6f220d066cae0ebc584bc92514494a9d6..2ff7a010c25d048371f3da855f0281708c834a9c 100644 (file)
@@ -187,7 +187,7 @@ compute_xte(struct xte *xte_rec)
     }
     // if timestamps exist, distance to interpolated point
     if (wpt1->GetCreationTime() != wpt2->GetCreationTime()) {
-      frac = (double)(wpt3->creation_time - wpt1->creation_time) /
+      frac = (double)(wpt3->GetCreationTime() - wpt1->GetCreationTime()) /
              (wpt2->GetCreationTime() - wpt1->GetCreationTime());
       linepart(wpt1->latitude, wpt1->longitude,
                wpt2->latitude, wpt2->longitude,
index 8dc79cfa60f2320a4fa7aa55ae43625fa32a18e2..85e03f6b0d157cbc9a49faf77f518c1e6cca4d53 100644 (file)
@@ -235,7 +235,7 @@ set_waypt_count(unsigned int nc)
 void
 waypt_disp(const waypoint *wpt)
 {
-  if (wpt->creation_time.isValid()) {
+  if (wpt->GetCreationTime().isValid()) {
     printf("%s ", qPrintable(wpt->creation_time.toString()));
   }
   printposn(wpt->latitude,1);